package com.liangzhong.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.global.GlobalDef;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.view.SmalLoadinglProgressBar;
	
	import lz.controls.Alert;
	
	import org.puremvc.as3.interfaces.INotification;

	/**
	 *模块应用mediator
	 * 资源加载等 
	 * @author lxh
	 */	
	public class ModuleMediator extends BaseMediator
	{
		/**
		 *指定模块子页面
		 * {type:0} 
		 */		
		public var param :Object;
		/**
		 *资源名称 
		 */		
		protected var resName :String;
		/**
		 *加载资源信息 
		 */		
		protected var resList :Array;
		/**
		 * 加载器
		 */		
		protected var loader :QueueLoader ; 
		/**
		 *上一进度下载数 
		 */		
		protected var preBytesLoaded :int;
		/**
		 * 已经加载的数据大小 
		 */		
		protected var bytesLoaded 	:int;
		/**
		 * 数据总大小 
		 */		
		protected var bytesTotal	:int;
		/**
		 * 当前加载的资源索引
		 */		
		protected var curLoaderIndex :int = 1;
		/**
		 * 资源总个数
		 */		
		protected var resCount		  :int = 1;
		
		public function ModuleMediator(mediatorName:String=null, 
									   viewComponent:Object=null ,
									   param :Object = null)
		{
			super(mediatorName, viewComponent);
			//
			resList = [];
			this.param = param;
		}
		/**
		 *加载模块资源 
		 * 
		 */
		public function loadRes():void
		{
			if(loader)
			{
				loader.addEventListener(QueueLoaderEvent.ITEM_COMPLETE,onLoadItemComplete);
				loader.addEventListener(QueueLoaderEvent.QUEUE_COMPLETE,onLoadComplete);
				loader.addEventListener(QueueLoaderEvent.ITEM_PROGRESS,onLoadItemProgress);
				loader.addEventListener(QueueLoaderEvent.ITEM_ERROR,onLoadItemError);
				loader.queueLoadStart();
			}
		}
		/**
		 * Load Item Progress
		 * @param e
		 * 
		 */		
		protected function onLoadItemProgress(e:QueueLoaderEvent):void
		{
			if(SmalLoadinglProgressBar.instance.parent == null)
			{
				GlobalDef.GameWindowContainer.addChild(SmalLoadinglProgressBar.instance);
			}
			//var info :String = int(e.bytesLoaded / e.bytesTotal * 100) + "%";
			bytesLoaded += (e.bytesLoaded - preBytesLoaded);
			var info :String = int(bytesLoaded / bytesTotal * 100) + "%";
			//trace("percent:",info,bytesLoaded,bytesTotal);
			preBytesLoaded = e.bytesLoaded;
			SmalLoadinglProgressBar.instance.setPercent(info);
		}
		/**
		 *item 加载出错 
		 * @param e
		 * 
		 */		
		protected function onLoadItemError(e :QueueLoaderEvent):void
		{
			var info :String = "加载出错\ninfo=" + e.path.url;
			Alert.show(info,"",Alert.YES,GlobalDef.GameStage);
		}
		/**
		 * Load Item Complete
		 * @param e
		 * 
		 */		
		protected function onLoadItemComplete(e :QueueLoaderEvent):void
		{
			curLoaderIndex ++;
			preBytesLoaded = 0;
			resList.push({id:e.queueVo.queueId,content:e.content});
		}
		/**
		 * Load Complete
		 * @param e
		 * 
		 */		
		protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			preBytesLoaded = 0;
			if(SmalLoadinglProgressBar.instance && SmalLoadinglProgressBar.instance.parent)
			{
				SmalLoadinglProgressBar.instance.parent.removeChild(SmalLoadinglProgressBar.instance);
			}
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MODULE_LOAD_RES_COMPLETE);
		}
		/**
		 *on remove mediator 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			resList = [];
			//
			if(loader)
			{
				loader.removeEventListener(QueueLoaderEvent.ITEM_COMPLETE,onLoadItemComplete);
				loader.removeEventListener(QueueLoaderEvent.QUEUE_COMPLETE,onLoadComplete);
				loader.removeEventListener(QueueLoaderEvent.ITEM_PROGRESS,onLoadItemProgress);
				loader.dispose();
				loader = null;
			}
		}
		/**
		 *监听的信息列表 
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [NotificationConstants.MATCH_START,
				NotificationConstants.MATCH_END];
		}
		/**
		 *设置是否缓存为位图 
		 * @param value
		 * 
		 */		
		public function setCacheAsBitmap(value :Boolean):void
		{
			if(viewComponent && viewComponent.hasOwnProperty("cacheAsBitmap"))
			{
				viewComponent["cacheAsBitmap"] = value;
			}
		}
		/**
		 * 捕获
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification :INotification):void
		{
			switch(notification.getName())
			{
				case NotificationConstants.MATCH_START://比赛开始
				{
					if(viewComponent && viewComponent.hasOwnProperty("visible"))
					{
						viewComponent["visible"] = false;
					}
					break;
				}
				case NotificationConstants.MATCH_END://比赛结束
				{
					if(viewComponent && viewComponent.hasOwnProperty("visible"))
					{
						viewComponent["visible"] = true;
					}
					break;
				}
			}
		}
	}
}